
package br.unirio.bsi.mono.ForumSite.action;

import br.unirio.bsi.mono.ForumSite.beans.util.copyBean;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

import br.unirio.bsi.mono.ForumSite.model.dao.GenericDAO;

import br.unirio.bsi.mono.ForumSite.beans.loginForm;
import br.unirio.bsi.mono.ForumSite.beans.formBeanU;

public class editUserAction extends org.apache.struts.action.Action 
{
  private final static String SUCCESS = "success";
  private final static String FAILURE = "failure";
  
  
  @Override
  public ActionForward execute(ActionMapping mapping, 
                               ActionForm form,
                               HttpServletRequest request, 
                               HttpServletResponse response) throws Exception 
  {      
    // pega a session
    HttpSession session = request.getSession();  
    
    // check logged
    loginForm lf = (loginForm)session.getAttribute("loginForm"); 
    if( lf == null || !lf.isLogged() )
      return mapping.findForward(FAILURE);
    
    // pega o form bean
    formBeanU euf = new formBeanU();
    
    copyBean.fromActionForm(euf, form);
    
    // instancia o DAO
    GenericDAO gD = GenericDAO.getInstance(getResources(request).getMessage("DB_NAME"));
    
    // set the ID for update
    euf.setId( lf.getId() );
    
    // do the update
    gD.update( euf ); 

    // update bean 
    copyBean.fromActionForm( lf, form );
    
    return mapping.findForward(SUCCESS);
  }
}
